flag = true;
    testo="";
    
    function def(v){
    	if(typeof v=='undefined')return false;
    	if(typeof v!='undefined'&&v!=null&&v!='')return true;
    }
function read() {
    if(typeof(window.localStorage) != 'undefined'){ 
        testo = window.localStorage.getItem ('dati'); 
        log=window.localStorage.getItem('log');
    	if (log==null || log==""){
    		log=false;
    		window.localStorage.setItem('log',log);
    	} else
    		log=log.bool();
    } 
    else{ 
        throw "window.localStorage, not defined"; 
    }
    loadFile();
}

String.prototype.bool = function() {
    return (/^true$/i).test(this);
};

function loadFile() {        
            if (log) console.log("readed");
            //alert(testo);
            if (testo == null && flag) {
                flag = false;                
                write();
            }            
                def($(testo).find("nreq").text())?nreq=$(testo).find("nreq").text():'';
                def($(testo).find("tema").text())?tema=$(testo).find("tema").text():'';
                def($(testo).find("carattere").text())?carDim=$(testo).find("carattere").text():'';
                def($(testo).find("storiaMax").text())?storiaMax=$(testo).find("storiaMax").text():'';                 
                indiceLoc = 0;
                $(testo).find("storico").each(function(){
                    $(this).find('dato').each(function(){
                        storicoPartenze[indiceLoc] =  $(this).attr('partenza');
                        storicoArrivi[indiceLoc] = $(this).attr('arrivo');
                        indiceLoc = indiceLoc + 1;
                    });
                    indice = indiceLoc;
                });
                $(testo).find("storicoDett").each(function(){
                    $(this).find('dato').each(function(){
                        storicoDett[indiceLoc] =  $(this).attr('treno');
                        storicoDesc[indiceLoc] = $(this).attr('desc');
                        indiceLoc = indiceLoc + 1;
                    });
                    indiceDett = indiceLoc;
                });
                if (log) console.log("lette" + " " + testo);
                setImp();
        }
           
function write() {   
    var v = new  XMLWriter();         
    v.writeStartDocument();
    v.writeStartElement( 'opzioni' );
    v.writeElementString('nreq',nreq);
    v.writeEndElement();
    v.writeElementString('tema',tema);
    v.writeEndElement();
    v.writeElementString('carattere',carDim);
    v.writeEndElement();
    v.writeElementString('storiaMax',storiaMax);
    v.writeEndElement();
    v.writeEndElement();   
    v.writeStartElement( 'storico' );
    for (i = 0; i < indice; i++) {       
        v.writeStartElement('dato');
        if(storicoPartenze[i] != 'undefined' && storicoArrivi[i]!='undefined'){
        	v.writeAttributeString( 'partenza', storicoPartenze[i]);
        	v.writeAttributeString( 'arrivo', storicoArrivi[i]);
        }
        v.writeEndElement();   }
    v.writeEndElement();   
    v.writeStartElement( 'storicoDett' );
    for (i = 0; i < indiceLoc; i++) {       
        v.writeStartElement('dato');
        if(storicoDett[i] != null && storicoDett[i] != '' && storicoDett[i] != 'undefined'){ 
        	v.writeAttributeString( 'treno', storicoDett[i]); 
        	if(storicoDesc[i] != 'undefined') v.writeAttributeString( 'desc', "");
        	else
        	   v.writeAttributeString( 'desc', storicoDesc[i]); 
        }
        v.writeEndElement();   }
    v.writeEndElement();   
    v.writeEndDocument();
    testo=v.flush();
    if(typeof(window.localStorage) != 'undefined'){ 
        window.localStorage.setItem('dati',testo); 
    } 
    else{ 
        throw "window.localStorage, not defined"; 
    }
    if (log) console.log("scrivo "+ testo);
    
    if (exitVar) {
    	 blackberry.app.exit();
    }
  }

